home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 60 < prev    next >
Text File  |  1996-08-06  |  2KB  |  64 lines

  1. Path: news.uni-stuttgart.de!schweikh
  2. From: schweikh@itosun.ito.uni-stuttgart.de (Jens Schweikhardt)
  3. Newsgroups: comp.std.c
  4. Subject: Re: int's and zero
  5. Date: 9 Jan 1996 11:44:42 GMT
  6. Organization: Comp.Center (RUS), U of Stuttgart, FRG
  7. Message-ID: <4ctkfa$1fj0@info4.rus.uni-stuttgart.de>
  8. References: <4cth4e$4q@odin.funcom.no>
  9. NNTP-Posting-Host: itosun.ito.uni-stuttgart.de
  10.  
  11. In article <4cth4e$4q@odin.funcom.no>,
  12. Eivind Eklund <eivind@odin.funcom.com> wrote:
  13. >Are zero in an integer required to be represented by binary zeros?
  14. >I know it is not required for floats and pointers, so memset()ing a 
  15. >structure to 8-bit zeros are not correct if it contain pointers or 
  16. >floating point values, but what about integers? Ie, if I have
  17. >
  18. >struct intstruct {
  19. >  int a;
  20. >  long b;
  21. >  short c;
  22. >};
  23. >..
  24. >struct intstruct teststruct;
  25. >memset(&teststruct, 0, sizeof(teststruct));
  26. >
  27. >Is teststruct.a, teststruct.b, and teststruct.c guaranteed to be == 0?
  28.  
  29. Eivind,
  30.  
  31. there is an (elegant, IMHO) way to initialize structures
  32. as if you had written <member> = 0 for any member.
  33. The standard requires this for objects of static storage type.
  34. It also has the advantage to work for pointers and floating point
  35. as well as numbers. It also applies recursively to structs and
  36. unions within a struct or union. Just make one
  37. structure static and assign it to whatever other (maybe
  38. automatic) struct you have. E.g.
  39.  
  40. static struct intstruct {
  41.   int a;
  42.   long b;
  43.   short c;
  44.   double d;
  45.   char *e;
  46. } zero;
  47.  
  48. ...
  49.  
  50. void
  51. foo (void)
  52. {
  53.     struct intstruct { ... } bar;
  54.     ...
  55.     bar = zero; /* Much more readable than memset(&bar, 0, sizeof bar); */
  56. }
  57.  
  58. Good compilers will certainly use a memcpy equivalent
  59. to code the strcuture assignment.
  60.  
  61. Bye, Jens
  62. -- 
  63. SIGSIG -- signature too long (core dumped)
  64.